package com.example.demo.componet;

import com.example.demo.model.CSVFileParam;
import com.example.demo.model.LayuiPage;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ReadCSVToBean {

    public static List<CSVFileParam> CSVBean(String path){
        BufferedReader br=null;
        String line = null;
        List<CSVFileParam> list = new ArrayList<CSVFileParam>();

        try {
            br = new BufferedReader(new FileReader(path));

            String[] head = br.readLine().split(",");  //先读一行除去表头
            while ((line = br.readLine()) != null) {
                //","取出任意两列
                CSVFileParam csvFileParam = new CSVFileParam();

                String[] split = line.split(",");

                csvFileParam.setId(Integer.parseInt(split[0]));  //
                csvFileParam.setEvent_time(split[1]);
                csvFileParam.setOrder_id(split[2]);
                csvFileParam.setProduct_id(split[3]);
                csvFileParam.setCategory_id(split[4]);
                csvFileParam.setCategory_code(split[5]);
                csvFileParam.setBrand(split[6]);
                csvFileParam.setPrice(Float.parseFloat(split[7]));
                csvFileParam.setUser_id(split[8]);
                csvFileParam.setAge(Integer.parseInt(split[9]));
                csvFileParam.setSex(split[10]);
                csvFileParam.setLocal(split[11]);

                list.add(csvFileParam);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return list;
    }

    public LayuiPage getLayuiPage(String path){
        LayuiPage layuiPage1 = new LayuiPage();
        layuiPage1.setCode(0);
        layuiPage1.setMsg("success");
        layuiPage1.setCount(this.CSVBean(path).size());
        layuiPage1.setData(this.CSVBean(path));
        return layuiPage1;
    }
}
